require 'View/UIBase/UIBase'

UIActivityLifeGift = UIBase.New(PanelNames.UIActivityLifeGift);
local this=UIActivityLifeGift
function this.Init(func,id)
    this.activeId=id
    UIBase.Init(UIActivityLifeGift, func);
end

function this:OnCreate()
    self.title=self:GetChild("title"):GetChild("title")
    self.coin=self:GetChild("CoinLb1")
    self.diamond=self:GetChild("CoinLb")
    self.btnLeft=self:GetChild("left")
    self.btnRight=self:GetChild("right")
    self.leftCtrl=self:GetController("leftshow")
    self.rightCtrl=self:GetController("rightshow")
    self.btnGet=self:GetChild("giftday")
    self.drawTip=self:GetChild("DrawTips")
    self.btnPreview=self:GetChild("Btn_Confirm")
    self.btnClose=self:GetChild("Backbtn")
    self.btnHelp=self:GetChild("help")
    self.jumpList=self:GetChild("list")
    self.lihui=self:GetChild("lihui")
    self.getTipCtrl=self:GetController("TipsType")
    self.jumpList:RemoveChildrenToPool()
    self.jumpData=logicMgr.CostManager.getFristClassData()
    self.jumpList.itemRenderer=function(index,item)
        self:SetJumpItem(index+1,item)
    end
    self.dayRewardList=self:GetChild("DayGift"):GetChild("itemListDay")
    self.btnDayReward=self:GetChild("DayGift"):GetChild("btn_Day")
    self.itemList=self:GetChild("itemList")
    self.btnItemList=self:GetChild("DayGift"):GetChild("btn_Day")
    self.btnShowCtrl=self:GetController("BtnShow")
    self.txtTips=self:GetChild("ItemTipsDay")
    self.jumpList.numItems=0
    self.jumpList.numItems=1
    self.drawWrapper = GoWrapper.New()
    self:addEvent();
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh)
end
function this:OnEnable()
    self.title.text=""
    self:Refresh(true)
end

function this:OnDisable()

end

function this:OnDestroy()

end
-------------------------------function--------------------------------------
function this:Refresh()
    self.tableData,self.netData,self.modelData=logicMgr.ActiveManager.getActiveData(self.activeId)
    if table.isNullOrEmpty(self.tableData) or table.isNullOrEmpty(self.netData) or table.isNullOrEmpty(self.modelData) then
        self:OnClickClose()
    end
    if not self.hasInitModel then
        self.hasInitModel=true
        self:InitModelData()
    end
    --self:ResetIndex()
    self:RefreshMoney()
    self.index=self.netData.index
    self:RefreshTodayReward()
    self:ChangeIndex(self.index)
    self.btnItemList.onClick:Clear()
    self.btnItemList.onClick:Add(function()
        ActivityHandler.sendReqActivityDrawRewardMessage({self.activeId})
    end)
end

function this:RefreshTodayReward()
    local todayGetCtrl=self:GetChild("DayGift"):GetController("Type")
    local nowData=self.tableData.showDatas[self.netData.index].f_Award
    local lastGetAward={}
    if self.netData.lastGetIndex>0 then
        lastGetAward=self.tableData.showDatas[self.netData.lastGetIndex].f_Award
    end
    if self.netData.todayState==0 then
        todayGetCtrl.selectedIndex=0
        local function ProcessAllAward()
            local info=clone(nowData)
            for i,v in ipairs(info)do
                v[2]=v[2]*self.netData.today
            end
            for i,v in ipairs(lastGetAward)do
                for a,b in ipairs(info)do
                    if b[1]==v[1]then
                        b[2]=b[2]-v[2]*self.netData.lastGetDay
                        break
                    end
                end
            end
            return info
        end
        local offsetData=self.netData.offsetData
        local showDatas=self.tableData.showDatas
        local function ProcessAward()
            local info= { }
            for i=self.netData.index,1,-1 do
                local rewards1=clone(showDatas[i].f_Award)
                local rewards2={}
                if showDatas[i-1] then
                    rewards2=clone(showDatas[i-1].f_Award)
                end
                local day
                if day~=self.netData.index then
                    day=(offsetData[i] or {}).days or 0
                else
                    day=self.netData.today
                end
                for _,b in ipairs(rewards1)do
                    b[2]=b[2]*day
                end
                for _,v in ipairs(rewards2)do
                    for _,b in ipairs(rewards1)do
                        if b[1]==v[1]then
                            b[2]=b[2]-v[2]*day
                            break
                        end
                    end
                end
                for _,v in ipairs(rewards1)do
                    for _,b in ipairs(info)do
                        if b[1]==v[1]then
                            b[2]=(b[2]+v[2])
                            break
                        end
                    end
                end
            end
            return info
        end
        local allAward=ProcessAllAward()
        local offSetReward=ProcessAward()
        for _,v in ipairs(offSetReward)do
            for _,b in ipairs(allAward)do
                if b[1]==v[1]then
                    b[2]=(b[2]-v[2])
                    break
                end
            end
        end
        local temp={}
        for i,v in ipairs(allAward)do
            if v[2]>0 then
                table.insert(temp,v)
            end
        end
        logicMgr.CostManager.RefreshItemList(self.dayRewardList, temp);
    else
        todayGetCtrl.selectedIndex=1
        logicMgr.CostManager.RefreshItemList(self.dayRewardList, nowData);
    end
    self.txtTips.text=string.format(getLanguage("ActivityDesc1500109"),getLanguage(self.tableData.showDatas[self.netData.index].f_Name))
end
function this:ChangeIndex()
    self.leftCtrl.selectedIndex=1
    self.rightCtrl.selectedIndex=1
    if self.index<=1 then
        self.index=1
        self.leftCtrl.selectedIndex=0
    end
    if self.index>=#self.tableData.showDatas-1 then
        self.index=#self.tableData.showDatas-1
        self.rightCtrl.selectedIndex=0
    end
    if self.index+1>self.netData.index then
        local nextNeed=self.tableData.showDatas[self.index+1].f_Price
        local nextName=getLanguage(self.tableData.showDatas[self.index+1].f_Name)
        local need=nextNeed-self.netData.money
        self.drawTip.text=string.format(getLanguage("ActivityDesc1500105"),need,nextName)
    else
        local name=getLanguage( self.tableData.showDatas[self.index+1].f_Name)
        self.drawTip.text=string.format(getLanguage("ActivityDesc1500104"),name)
    end
    local offsetData=self.netData.offsetData
    if self.index+1==self.netData.index then
        if self.netData.todayState==0 then
            self.getTipCtrl.selectedIndex=1
        else
            self.getTipCtrl.selectedIndex=2
        end
    elseif self.index+1<self.netData.index then
        if self.index+1<=self.netData.lastGetIndex then
            self.getTipCtrl.selectedIndex=2
        else
            self.getTipCtrl.selectedIndex=1
        end
    else
        self.getTipCtrl.selectedIndex=3
    end
    self:RefreshItemList(self.index+1) --showDatas发过来是从第一级开始，但是列表是从第二级开始
end
function this:RefreshItemList(index)
    local data=self.tableData.showDatas[index].f_Award
    local lastData=self.tableData.showDatas[index-1].f_Award
    local id=self.tableData.showDatas[index].f_Id
    local lastId=self.tableData.showDatas[index-1].f_Id
    local offsetData=self.netData.offsetData
    local offsetDay=1
    if offsetData[id]~=nil then
        offsetDay= offsetData[id].days
    elseif id>=self.netData.stage then
        offsetDay=self.netData.today
    end
    self.itemList:RemoveChildrenToPool()
    local function ProcessAward()
        local info=clone(data)
        for a,b in ipairs(info)do
            b[2]=b[2]*offsetDay
        end
        for i,v in ipairs(lastData)do
            for a,b in ipairs(info)do
                if b[1]==v[1]then
                    b[2]=b[2]-v[2]*offsetDay
                    break
                end
            end
        end
        local temp={}
        for i,v in ipairs(info)do
            if v[2]>0 then
                table.insert(temp,v)
            end
        end
        return temp
    end
    local info=ProcessAward()
    for i,v in ipairs(info)do
        local item=self.itemList:AddItemFromPool()
        logicMgr.CostManager.RefreshItem(item:GetChild("Item"), info[i]);
    end
end

function this:SetJumpItem(index,itemObj)
    itemObj.onClick:Clear()
    itemObj.onClick:Add(function()
        local data = logicMgr.CostManager.getCostDataByFristIndexAndSecondIndex(index, 1);
        if data.id ~= nil then
            showUI(PanelNames.UICost, nil,data.id);
        end
    end)
end
function this:OnClickClose()
    self:OnBack()
end
function this:addEvent()
    self:RegisterOnClick(self.btnLeft,self.OnClickBtnLeft)
    self:RegisterOnClick(self.btnRight,self.OnClickBtnRight)
    self:RegisterOnClick(self.btnGet,self.OnClickBtnGet)
    self:RegisterOnClick(self.btnPreview,self.OnClickBtnPreview)
    self:RegisterOnClick(self.btnClose,self.OnClickClose)
    self:RegisterOnClick(self.btnHelp,self.OnClickBtnHelp)
    self:RegisterOnClick(self.btnItemList,self.OnClickBtnItemList)
end
function this:SetModel()

end
function this:OnClickBtnItemList()
    self:OnClickBtnGet()
end
function this:OnClickBtnHelp()
    if self.modelData then
        logicMgr.UIShowManager.ShowCommonTips(self.modelData.f_ExplainTitle,self.modelData.f_ExplainText)
    end
end
function this:OnClickBtnPreview()
    showUI(PanelNames.UIActivityLifeGiftTips,nil,self.activeId)
end
function this:OnClickBtnGet()

end

function this:OnClickBtnLeft()
    self.index=self.index-1
    self:ChangeIndex(self.index)
end

function this:OnClickBtnRight()
    self.index=self.index+1
    self:ChangeIndex(self.index)
end

function this:ResetIndex()

end
function this:InitModelData()
    local function SetLanguage(name,language)
        self:GetChild(name).text=getLanguage(language)
    end
    self:GetChild("title"):GetChild("title").text=getLanguage(self.modelData.f_titleName)
    SetLanguage("titletips",self.modelData.f_TitleTips)
    --SetLanguage("titletips1",self.modelData.f_TitleGetTips)
    SetLanguage("TextPatch",self.modelData.f_TitleDownTips)
    self:GetChild("Banner").url=self.modelData.f_TitlePic
    self:GetChild("bg").url=self.modelData.f_Banner
    local path = HeroManager.GetHeroDrawingPath(self.modelData.f_Hero);
    resMgr:LoadModelPrefab(path, function(go)
        if(self.RoleLihui~=nil)then
            destroyImmediate(self.RoleLihui);
            self.RoleLihui = nil
        end
        self.RoleLihui = newObject(go);
        self.RoleLihui.name = path
        Util.AddAction(self.RoleLihui,"idle1",true,1);
        self.RoleLihui:AddComponent(TCanvas);
        self.drawWrapper:SetWrapTarget(self.RoleLihui,false);
        Util.Lua_SetLocalPositionXYZ(self.RoleLihui.transform,0,0,0)
        Util.Lua_SetLocalScaleXYZ(self.RoleLihui.transform,self.lihui.scaleX,self.lihui.scaleY,1.1)
        self.lihui:SetNativeObject(self.drawWrapper);
    end)
end
function this:RefreshMoney()
    self.diamond:GetChild("icon").icon = "ui://ItemAtlas/"..CurrencyEnum.GOLD;
    self.diamond:GetChild("title").text = logicMgr.ItemManager.getCurrencyCountByModelId(1);
    self.coin:GetChild("icon").icon = "ui://ItemAtlas/"..CurrencyEnum.MONEY;
    self.coin:GetChild("title").text = logicMgr.ItemManager.getCurrencyCountByModelId(3);
end
return this;